<?php
namespace app\sports\match\service;

use app\sports\match\model\JCModel;
use app\sports\team\service\TeamService;
use think\Db;

class JCService
{
    /**
     * 获取竞彩列表
     */
    public function getJCList($type, $page = 1, $count = 10)
    {
        if (!in_array($type, ['jczq', 'jclq'])) {
            return ['code' => -1, 'msg' => '不存在的type类型'];
        }

        $lotteryType = 0;
        switch ($type) {
            case 'jczq':
                $lotteryType = 101;
                break;
            case 'jclq':
                $lotteryType = 201;
                break;
        }

        //获取竞彩数据
        $jcModel = new JCModel();
        $data = $jcModel->getJCList($type, $page, $count);
        if ($data['code'] != 0) {
            return $data;
        }

        $jcList = $data['data']['list'];
        $count = $data['data']['count'];

        //关联取得竞彩比赛数据
        $naMiId = array_column($jcList, 'nami_id');
        $data = $jcModel->getJCLinkListByNaMiId($lotteryType, $naMiId);
        $jcLinkList = $data['data']['list'];

        foreach ($jcList as $key => $value) {
            if (isset($jcLinkList[$value['nami_id']])) {
                $jcList[$key]['match_id'] = $jcLinkList[$value['nami_id']]['match_id'];
            } else {
                $jcList[$key]['match_id'] = 0;
            }

            $jcList[$key]['match_time'] = date('Y-m-d', $value['match_time']);
            $jcList[$key]['issue_num'] = $this->_handleMatchIssue($value['issue_num']);
            $spfArr = explode(',', $value['spf']);
            array_unshift($spfArr, "0");
            $jcList[$key]['spf_arr'] = $spfArr;
            $jcList[$key]['rq_arr'] = explode(',', $value['rq']);
        }

        return ['code' => 0, 'data' => ['list' => $jcList, 'count' => $count]];
    }

    /**
     * 处理赛事编号
     */
    private function _handleMatchIssue($issueNum)
    {
        $day = $issueNum[0];
        $newDay = '';
        switch ($day) {
            case 1:
                $newDay = '周一';
                break;
            case 2:
                $newDay = '周二';
                break;
            case 3:
                $newDay = '周三';
                break;
            case 4:
                $newDay = '周四';
                break;
            case 5:
                $newDay = '周五';
                break;
            case 6:
                $newDay = '周六';
                break;
            case 7:
                $newDay = '周日';
                break;
        }

        $last = substr($issueNum, 1);

        return $newDay . $last;
    }

    /**
     * 获取单场竞彩的信息
     */
    public function getJingCaiSimpleByMatchId($type, $matchId)
    {
        $lotteryType = 0;
        switch ($type) {
            case 'jczq':
                $lotteryType = 101;
                break;
            case 'jclq':
                $lotteryType = 201;
                break;
        }

        $jcModel = new JCModel();
        $jcLinkData = $jcModel->getJCLinkByMatchId($lotteryType, $matchId);

        if ($jcLinkData['code'] != 0) {
            return $jcLinkData;
        }

        if (empty($jcLinkData['data'])) {
            return ['code' => -1, 'msg' => '未找到该场比赛 -1'];
        }
        $jcLinkData = $jcLinkData['data'];

        $namiId = $jcLinkData['nami_id'];

        $data = $jcModel->getJcByNaMiId($type, $namiId);
        if ($data['code'] != 0) {
            return $data;
        }

        if (empty($data['data'])) {
            return ['code' => -1, 'msg' => '未找到该场比赛 -2'];
        }

        $data = $data['data'];
        $data['match_time_date'] = date('Y-m-d H:i:s', $data['match_time']);
        $data['issue_num'] = $this->_handleMatchIssue($data['issue_num']);

        $spfArr = explode(',', $data['spf']);
        array_unshift($spfArr, "0");
        $data['spf_arr'] = $spfArr;
        $data['rq_arr'] = explode(',', $data['rq']);

        //获取球队信息
        $matchService = new MatchService();
        $matchInfo = $matchService->getMatchInfoByMatchId($matchId);
        if (empty($matchInfo)) {
            return ['code' => -1, 'msg' => '未找到该场比赛'];
        }

        $leftTeamId = $matchInfo['left_team_id'];
        $rightTeamId = $matchInfo['right_team_id'];

        $teamService = new TeamService();
        $leftTeamInfo = $teamService->getTeamInfoByTeamId($leftTeamId);
        $data['short_home_logo'] = isset($leftTeamInfo['logo']) ? $leftTeamInfo['logo'] : '';

        $teamService = new TeamService();
        $rightTeamInfo = $teamService->getTeamInfoByTeamId($rightTeamId);
        $data['short_away_logo'] = isset($rightTeamInfo['logo']) ? $rightTeamInfo['logo'] : '';

        return ['code' => 0, 'data' => $data];
    }

    /**
     * 获取多场竞彩的信息
     */
    public function getJcListByMatchId($type, $matchId)
    {
        $lotteryType = 0;
        switch ($type) {
            case 'jczq':
                $lotteryType = 101;
                break;
            case 'jclq':
                $lotteryType = 201;
                break;
        }

        //取得竞彩关联数据
        $jcModel = new JCModel();
        $data = $jcModel->getJCLinkListByMatchId($lotteryType, $matchId);
        if ($data['code'] != 0) {
            return $data;
        }
        $jcLinkList = $data['data']['list'];
        $jcLinkListRes = [];
        foreach ($jcLinkList as $value) {
            $jcLinkListRes[$value['nami_id']] = $value['match_id'];
        }
        $namiId = array_column($jcLinkList, 'nami_id');

        //取得竞彩数据
        $data = $jcModel->getJcListByNaMiId($type, $namiId);
        if ($data['code'] != 0) {
            return $data;
        }
        $jcList = $data['data']['list'];
        $jcListRes = [];
        foreach ($jcList as $key => $value) {
            if (isset($jcLinkListRes[$value['nami_id']])) {
                $jcListRes[$jcLinkListRes[$value['nami_id']]] = $value;
                $jcListRes[$jcLinkListRes[$value['nami_id']]]['match_id'] = $jcLinkListRes[$value['nami_id']];
            }
        }

        return ['code' => 0, 'data' => $jcListRes];
    }
}